package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.mapper.BasicCarrierMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import  cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 承运商档案 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2022-12-29
 */
@Service
public class BasicCarrierServiceImpl extends ServiceImpl<BasicCarrierMapper, BasicCarrier> implements BasicCarrierService {

    @Override
    public void add(BasicCarrierParam param){
        BasicCarrier entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicCarrierParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicCarrierParam param){
        BasicCarrier oldEntity = getOldEntity(param);
        BasicCarrier newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicCarrierResult findBySpec(BasicCarrierParam param,Long operateId){
        return this.baseMapper.findBySpec(param,operateId);
    }

    @Override
    public BasicCarrierResult findByCarrierName(String carrierName, Long operateId) {
        return this.baseMapper.findByCarrierName(carrierName,operateId);
    }

    @Override
    public List<BasicCarrierResult> findListBySpec(BasicCarrierParam param,Long operateId){
        return this.baseMapper.findListBySpec(param,operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicCarrierParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicCarrierParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicCarrier getOldEntity(BasicCarrierParam param) {
        return this.getById(getKey(param));
    }

    private BasicCarrier getEntity(BasicCarrierParam param) {
        BasicCarrier entity = new BasicCarrier();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
